#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Author : zaniu(zzaniu@126.com)
# @Time   : 2021/6/23 14:51 
# @Desc   :
# 
#                   _ooOoo_
#                  o8888888o
#                  88" . "88
#                  (| -_- |)
#                  O\  =  /O
#               ____/`---'\____
#             .'  \\|     |//  `.
#            /  \\|||  :  |||//  \
#           /  _||||| -:- |||||-  \
#           |   | \\\  -  /// |   |
#           | \_|  ''\---/''  |   |
#           \  .-\__  `-`  ___/-. /
#         ___`. .'  /--.--\  `. . __
#      ."" '<  `.___\_<|>_/___.'  >'"".
#     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
#     \  \ `-.   \_ __\ /__ _/   .-` /  /
# =====`-.____`-.___\_____/___.-`____.-'=====
#                   `=---='
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#             佛祖保佑       永无BUG
#
import os
from concurrent import futures
import grpc

from learn_grpc_001.proto import hello_pb2_grpc, hello_pb2


class Greeter(hello_pb2_grpc.GreeterServicer):
    def SayHello(self, request, context):
        return hello_pb2.HelloReply(message='Hello, %s!' % request.name)


class AddServer(hello_pb2_grpc.AddServerServicer):

    def __init__(self):
        self.a = 0

    def AddInt(self, request, context):
        print(f"request = {request}, context = {context}")
        # context.set_code(grpc.StatusCode.CANCELLED)
        # context.set_details("沙雕啊")
        return hello_pb2.AddIntReply(message=request.value1 + request.value2)

    def AddFloat(self, request, context):
        return hello_pb2.AddFloatReply(message=request.value1 + request.value2)


def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=os.cpu_count()))
    hello_pb2_grpc.add_AddServerServicer_to_server(AddServer(), server)
    hello_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
    server.add_insecure_port('[::]:50051')
    server.start()
    server.wait_for_termination()


if __name__ == '__main__':
    serve()
